<?php 
//辅助类
if ( ! function_exists('is_email'))
{
    function is_email($val)
    {
        return preg_match('/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/i',$val);
    }
}


if ( ! function_exists('is_mobile'))
{
    function is_mobile($val)
    {
        return preg_match('^1[3458]\d{9}$',$val);
    }
}
if(!function_exists('trim_space'))
{
    function trim_space($data)
    {
        if(is_array($data))
        {
            return array_map('trim',$data);
        }
        return trim($data);
    }
}

if (!function_exists('is_account')) {
    //只能是电子邮件或手机号或以字母开头的字母数字下划线混合，长度在6-15位之间
    function is_account($account)
    {
       return preg_match('/(^[a-zA-Z]{1}[a-zA-Z0-9_]{5,14}$)|(^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$)|(^1[3458]\d{9}$)/', $account);
    }
}


if(!function_exists('create_guid'))
{
    function create_guid() {
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = chr(123)// "{"
        .substr($charid, 0, 8).$hyphen
        .substr($charid, 8, 4).$hyphen
        .substr($charid,12, 4).$hyphen
        .substr($charid,16, 4).$hyphen
        .substr($charid,20,12)
        .chr(125);// "}"
        return str_replace(array(chr(45),chr(123),chr(125)),array('','',''),$uuid);
    }
}


if(!function_exists('is_ip'))
{
    function is_ip($ip)
    {
        return preg_match('/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/',$ip);
    }
}


/*身份证号码验证*/

function checkID ($sfznum,$len='both') {
  if (strlen($sfznum)==15 && $len=='both') {    //当$len不等于'both'时，15位号码无效
   $truenum=substr($sfznum,0,6).'19'.substr($sfznum,6);             //为返回18位号码作准备。
   $preg="/^[\d]{8}((0[1-9])|(1[0-2]))((0[1-9])|([12][\d])|(3[01]))[\d]{3}$/";
  } elseif (strlen($sfznum)==18) {
   $truenum=substr($sfznum,0,17);
   $preg="/^[\d]{6}((19[\d]{2})|(200[0-8]))((0[1-9])|(1[0-2]))((0[1-9])|([12][\d])|(3[01]))[\d]{3}[0-9xX]$/";
  } else {
   return false;
  }
  if (!preg_match($preg,$sfznum)) return false;   //完成正则表达式检测

  /*----------- 以下计算第18位验证码-------------*/
  $nsum = substr($truenum, 0,1)*7;
  $nsum = $nsum + substr($truenum, 1,1)*9;
  $nsum = $nsum + substr($truenum, 2,1)*10;
  $nsum = $nsum + substr($truenum, 3,1)*5;
  $nsum = $nsum + substr($truenum, 4,1)*8;
  $nsum = $nsum + substr($truenum, 5,1)*4;
  $nsum = $nsum + substr($truenum, 6,1)*2;
  $nsum = $nsum + substr($truenum, 7,1)*1;
  $nsum = $nsum + substr($truenum, 8,1)*6;
  $nsum = $nsum + substr($truenum, 9,1)*3;
  $nsum = $nsum + substr($truenum,10,1)*7;
  $nsum = $nsum + substr($truenum,11,1)*9;
  $nsum = $nsum + substr($truenum,12,1)*10;
  $nsum = $nsum + substr($truenum,13,1)*5;
  $nsum = $nsum + substr($truenum,14,1)*8;
  $nsum = $nsum + substr($truenum,15,1)*4;
  $nsum = $nsum + substr($truenum,16,1)*2;
  $yzm  = 12 - $nsum % 11;
  if($yzm==10)$yzm='x';
  elseif($yzm==12)$yzm='1';
  elseif($yzm==11)$yzm='0';
  /*----------18位验证码计算完成-------------*/
  if (strlen($sfznum)==18) {
   if (substr($sfznum,17,1)!=$yzm) return false;
  }
  return $truenum.$yzm;
 }